{
 "cells": [
  {
   "cell_type": "raw",
   "id": "641f8cb0",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_label: Azure OpenAI\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38f26d7a",
   "metadata": {},
   "source": [
    "# AzureChatOpenAI\n",
    "\n",
    ">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n",
    "\n",
    "This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. We recommend having version `openai>=1` installed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "96164b42",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from langchain.chat_models import AzureChatOpenAI\n",
    "from langchain.schema import HumanMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
    "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8161278f",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AzureChatOpenAI(\n",
    "    openai_api_version=\"2023-05-15\",\n",
    "    azure_deployment=\"your-deployment-name\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "99509140",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content=\"J'adore la programmation.\")"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message = HumanMessage(\n",
    "    content=\"Translate this sentence from English to French. I love programming.\"\n",
    ")\n",
    "model([message])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f27fa24d",
   "metadata": {},
   "source": [
    "## Model Version\n",
    "Azure OpenAI responses contain `model` property, which is name of the model used to generate the response. However unlike native OpenAI responses, it does not contain the version of the model, which is set on the deployment in Azure. This makes it tricky to know which version of the model was used to generate the response, which as result can lead to e.g. wrong total cost calculation with `OpenAICallbackHandler`.\n",
    "\n",
    "To solve this problem, you can pass `model_version` parameter to `AzureChatOpenAI` class, which will be added to the model name in the llm output. This way you can easily distinguish between different versions of the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0531798a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.callbacks import get_openai_callback"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aceddb72",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = AzureChatOpenAI(\n",
    "    openai_api_version=\"2023-05-15\",\n",
    "    azure_deployment=\"gpt-35-turbo\",  # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
    ")\n",
    "with get_openai_callback() as cb:\n",
    "    model([message])\n",
    "    print(\n",
    "        f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n",
    "    )  # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e61eefd",
   "metadata": {},
   "source": [
    "We can provide the model version to `AzureChatOpenAI` constructor. It will get appended to the model name returned by Azure OpenAI and cost will be counted correctly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8d5e54e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total Cost (USD): $0.000044\n"
     ]
    }
   ],
   "source": [
    "model0613 = AzureChatOpenAI(\n",
    "    openai_api_version=\"2023-05-15\",\n",
    "    deployment_name=\"gpt-35-turbo\",\n",
    "    model_version=\"0613\",\n",
    ")\n",
    "with get_openai_callback() as cb:\n",
    "    model0613([message])\n",
    "    print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
